let timer, flag;
let timeout = null;
export const globalfun = {
	userStatus: (status) => {
		switch (status) {
			case 0:
				return '游客';
			case 1:
				return '稻友';
			case 2:
				return '稻使';
			case 3:
				return '社长';
			default:
				return '';
		}
	},
	formatStatus: (status) => {
		switch (status) {
			case '待审核':
				return 'pending';
			case '已通过':
				return 'approved';
			case '已驳回':
				return 'rejected';
			default:
				return '';
		}
	},
	formatTime: (timeStr, type = 'all') => {
		const iosSafeDateStr = timeStr.replace(' ', 'T');
		if (!iosSafeDateStr) {
			console.warn('日期字符串为空');
			return '';
		}
		const date = new Date(iosSafeDateStr);
		// 基础时间信息
		const year = date.getFullYear();
		const month = date.getMonth() + 1;
		const day = date.getDate();
		const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
		const weekDay = weekDays[date.getDay()];
		const hours = date.getHours().toString().padStart(2, '0');
		const minutes = date.getMinutes().toString().padStart(2, '0');
		const addZero = (num) => num < 10 ? `0${num}` : num;
		// 根据类型返回对应结果
		switch (type) {
			case 'year':
				return year;
			case 'month-day':
				return `${month}.${addZero(day)}`;
			case 'hours-minutes':
				return `${hours}:${minutes}`;
			case 'week-day':
				return weekDay;
			case 'all':
			default:
				return {
					year,
					monthDay: `${month}.${day}`,
						hoursMinutes: `${hours}:${minutes}`,
						weekDay
				};
		}
	},
	formatDateTime: (dateStr) => {
		if(dateStr == ''){
			return '---'
		}
		const iosSafeDateStr = dateStr.replace(' ', 'T');
		// 1. 检查输入是否有效
		if (!iosSafeDateStr) {
			console.warn('日期字符串为空');
			return '';
		}

		// 2. 创建 Date 对象
		const date = new Date(iosSafeDateStr);

		// 3. 再次检查日期是否有效 (防止 invalid date)
		if (isNaN(date.getTime())) {
			console.warn(`无效的日期字符串: ${iosSafeDateStr}`);
			return '';
		}

		// 4. 定义星期几的中文数组
		const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];

		// 5. 获取各个部分的值
		const month = date.getMonth() + 1; // getMonth() 返回 0-11，需 +1
		const day = date.getDate();
		const weekDay = weekDays[date.getDay()]; // getDay() 返回 0 (周日) 到 6 (周六)
		const hours = date.getHours();
		const minutes = date.getMinutes();

		// 6. 辅助函数：为个位数添加前导零
		const addZero = (num) => num < 10 ? `0${num}` : num;

		// 7. 拼接并返回结果
		return `${addZero(month)}/${addZero(day)} ${weekDay} ${addZero(hours)}:${addZero(minutes)}`;
	},
	// 在规定时间内 无论用户点击了多少次 函数只触发一次
	throttling: function(func, interval = 1000) {
		try {
			if (!flag) {
				flag = true;
				typeof func === 'function' && func();
				timer = setTimeout(() => {
					flag = false;
					timer = null; // 重置定时器，避免内存泄漏
				}, interval);
			}
		} catch (e) {
			console.error('节流函数错误：', e);
		}
	},
	// 在规定时间内 用户导致动作被连续频繁触发的情况下 函数只会被执行一次
	// 防抖函数（修正命名：chattering 改为 debounce 更规范）
	debounce: function(func, interval = 1000) {
		try {
			if (timeout !== null) {
				clearTimeout(timeout);
				timeout = null; // 清除时重置，避免内存泄漏
			}
			timeout = setTimeout(() => {
				typeof func === 'function' && func();
				timeout = null; // 执行后重置
			}, interval);
		} catch (e) {
			console.error('防抖函数错误：', e);
		}
	},
	pageGo: url => {
		uni.navigateTo({
			url
		})
	},
	pageSwitchTab: url => {
		uni.switchTab({
			url
		})
	},
	pageBack: () => {
		uni.navigateBack()
	},
	phoneCall: number => {
		uni.makePhoneCall({
			phoneNumber: number, //仅为示例
			success: function() {
				console.log('success');
			}
		});
	},
	opennavigation: data => {
		uni.openLocation({
			latitude: parseFloat(data.latitude),
			longitude: parseFloat(data.longitude),
			success: function() {
				console.log('success');
			}
		});
	},

	previewImage: (list, index = 0) => {
		uni.previewImage({
			current: index,
			urls: list,
			longPressActions: {
				itemList: ['发送给朋友', '保存图片', '收藏'],
				success: function(data) {
					console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
				},
				fail: function(err) {
					console.log(err.errMsg);
				}
			}
		});
	},
	copyFun: data => {
		uni.setClipboardData({
			data, // 要复制的内容
			success: () => { // 复制成功回调
				uni.showToast({
					title: '已复制',
					icon: 'success'
				})
			}
		});
	}
}